package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.base.util.CreateOssUtil;
import com.coding.dao.*;
import com.coding.pojo.*;
import com.coding.service.PostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/7/28
 **/
@Service
public class PostImpl implements PostService {
    @Autowired
    PostDao postDao;
    @Autowired
    GroupDao groupDao;
    @Autowired
    TheclassDao theclassDao;
    @Autowired
    UserDao userDao;
    @Autowired
    OssImagesDao ossImagesDao;
    @Autowired
    CollectDao collectDao;
    @Autowired
    ThumbDao thumbDao;
    @Autowired
    CommentDao commentDao;
    @Autowired
    MainPlaceDao mainPlaceDao;
    @Autowired
    PostphotoDao postphotoDao;

    @Override
    public void getlistbyselect(int user_id, Post post, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Post> list = postDao.queryAlllist(post);
        //加入班级和圈子信息
        for (Post newPost : list
        ) {
            if (newPost.getPost_user_id() != null) {
                User user = userDao.getuserbyuser_id(newPost.getPost_user_id());
                if (user != null) {
                    newPost.setUser(user);
                }
            }
            ;
            if (newPost.getPost_group_id() != null) {
                Group group = groupDao.queryById(newPost.getPost_group_id());
                if (group != null) {
                    newPost.setGroup(group);
                }
            }
            if (newPost.getPost_class_id() != null) {
                Theclass theclass = theclassDao.queryById(newPost.getPost_class_id());
                if (theclass != null) {
                    newPost.setTheclass(theclass);
                }
            }
            int collectNumbers = collectDao.countCollectTimes(newPost.getPost_id(), "post");
            if (collectNumbers > 0) {
                newPost.setPost_collect(collectNumbers);
            } else {
                newPost.setPost_collect(0);
            }
            int thumbNumbers = thumbDao.countThumb(newPost.getPost_id(), "post");
            if (thumbNumbers > 0) {
                newPost.setPost_likes(thumbNumbers);
            } else {
                newPost.setPost_likes(0);
            }
            int commentNumbers = commentDao.countComments(newPost.getPost_id(), "post");
            if (commentNumbers > 0) {
                newPost.setPost_comments(commentNumbers);
            } else {
                newPost.setPost_comments(0);
            }
            Collect collect = collectDao.selectAll(user_id, newPost.getPost_id(), "post");
            if (collect != null) {
                newPost.setPost_collect_status(1);
            }
            Thumb thumb = thumbDao.selectAll(user_id, newPost.getPost_id(), "post");
            if (thumb != null) {
                newPost.setPost_likes_status(1);
            }

            //获得图片列表
            List<Postphoto> postphotos = postphotoDao.selectByPost(newPost.getPost_id());
            if (postphotos != null) {
                List<OssImages> ossImagesList = new ArrayList<>();
                for (Postphoto p : postphotos
                ) {
                    OssImages ossImages = ossImagesDao.selectByPrimaryKey(p.getImage_id());
                    ossImagesList.add(ossImages);
                }
                newPost.setOssImagesList(ossImagesList);
            }
        }
        PageInfo pageInfo = new PageInfo(list);
        list = pageInfo.getList();
        if (list.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的帖子");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的帖子共" + list.size() + "条", list);
    }

    @Override
    public void getPostbyselect(int user_id, int post_id, BaseModel baseModel) throws Exception {
        Post post = postDao.selectByPrimaryKey(post_id);
        if (post != null) {
            if (post.getPost_user_id() != null) {
                User user = userDao.getuserbyuser_id(post.getPost_user_id());
                if (user != null) {
                    post.setUser(user);
                }
            }

            if (post.getPost_group_id() != null) {
                Group group = groupDao.queryById(post.getPost_group_id());
                if (group != null) {
                    post.setGroup(group);
                }
            }
            if (post.getPost_class_id() != null) {
                Theclass theclass = theclassDao.queryById(post.getPost_class_id());
                if (theclass != null) {
                    post.setTheclass(theclass);
                }
            }
            int collectNumbers = collectDao.countCollectTimes(post_id, "post");

            if (collectNumbers > 0) {
                post.setPost_collect(collectNumbers);
            } else {
                post.setPost_collect(0);
            }
            int thumbNumbers = thumbDao.countThumb(post_id, "post");
            if (thumbNumbers > 0) {
                post.setPost_likes(thumbNumbers);
            } else {
                post.setPost_likes(0);
            }
            int commentNumbers = commentDao.countComments(post_id, "post");
            if (commentNumbers > 0) {
                post.setPost_comments(commentNumbers);
            } else {
                post.setPost_comments(0);
            }
            Collect thecollect = collectDao.selectAll(user_id, post_id, "post");
            if (thecollect != null) {
                post.setPost_collect_status(1);
            }
            Thumb thumb = thumbDao.selectAll(user_id, post_id, "post");
            if (thumb != null) {
                post.setPost_likes_status(1);
            }
            //获得图片列表
            List<Postphoto> postphotos = postphotoDao.selectByPost(post_id);
            if (postphotos != null) {
                List<OssImages> ossImagesList = new ArrayList<>();
                for (Postphoto p : postphotos
                ) {
                    OssImages ossImages = ossImagesDao.selectByPrimaryKey(p.getImage_id());
                    ossImagesList.add(ossImages);
                }
                post.setOssImagesList(ossImagesList);
            }

            baseModel.setSucceed("成功获得帖子", post);
        } else {
            baseModel.setFailure("获得帖子失败");
        }


    }

    @Override
    public void insertPost(Post post, MultipartFile file, BaseModel baseModel) throws Exception {
        if (file == null || file.getSize() <= 0) {
            baseModel.setFailure("file不能为空");
            return;
        }
        //上传图片至oss
        OssImages ossImages = CreateOssUtil.createOss(file);
        if (ossImages == null || ossImages.getImage_url() == null) {
            baseModel.setFailure("上传图片失败");
            return;
        }
        if (ossImagesDao.insert(ossImages) == 1) {
            post.setPost_image_id(ossImages.getImage_id());
        }
        List<Group> groupList = groupDao.queryByGroupName(post.getPost_more2());
        if (groupList.size() > 0 && groupList != null) {
            post.setPost_group_id(groupList.get(0).getGroupId());
        }
        List<MainPlace> mainPlaceList = mainPlaceDao.searchMainPlace(post.getPost_more2());
        if (mainPlaceList.size() > 0 && mainPlaceList != null) {
            post.setPost_place_id(mainPlaceList.get(0).getMain_id());
        }
        if (postDao.insertSelective(post) == 1) {
            Post newPost = postDao.selectByPrimaryKey(post.getPost_id());
            if (newPost.getPost_user_id() != null) {
                User user = userDao.getuserbyuser_id(newPost.getPost_user_id());
                if (user != null) {
                    newPost.setUser(user);
                }
            }
            ;
            if (newPost.getPost_group_id() != null) {
                Group group = groupDao.queryById(newPost.getPost_group_id());
                if (group != null) {
                    newPost.setGroup(group);
                }
            }
            if (newPost.getPost_class_id() != null) {
                Theclass theclass = theclassDao.queryById(newPost.getPost_class_id());
                if (theclass != null) {
                    newPost.setTheclass(theclass);
                }
            }
            baseModel.setSucceed("成功新增帖子", newPost);
        } else {
            baseModel.setFailure("新增帖子失败");
        }
    }

    @Override
    public void insertPostMoreImage(Post post, MultipartFile[] files, BaseModel baseModel) throws Exception {
        if (files == null ) {
            baseModel.setFailure("file不能为空");
            return;
        }
        if (files.length==0){
            baseModel.setFailure("files的长度为0");
            return;
        }
        //上传图片至oss
        List<OssImages> ossImagesList = CreateOssUtil.creatOssImages(files);
        for (OssImages o : ossImagesList
        ) {
            ossImagesDao.insert(o);
        }


        if (ossImagesList.size() > 0) {
            post.setPost_image_id(ossImagesList.get(0).getImage_id());
        } else {
            baseModel.setFailure("上传失败");
            return;
        }
        List<Group> groupList = groupDao.queryByGroupName(post.getPost_more2());
        if (groupList.size() > 0 && groupList != null) {
            post.setPost_group_id(groupList.get(0).getGroupId());
        }
        List<MainPlace> mainPlaceList = mainPlaceDao.searchMainPlace(post.getPost_more2());
        if (mainPlaceList.size() > 0 && mainPlaceList != null) {
            post.setPost_place_id(mainPlaceList.get(0).getMain_id());
        }
        if (postDao.insertSelective(post) == 1) {
            Post newPost = postDao.selectByPrimaryKey(post.getPost_id());
            if (newPost.getPost_user_id() != null) {
                User user = userDao.getuserbyuser_id(newPost.getPost_user_id());
                if (user != null) {
                    newPost.setUser(user);
                }
            }
            ;
            if (newPost.getPost_group_id() != null) {
                Group group = groupDao.queryById(newPost.getPost_group_id());
                if (group != null) {
                    newPost.setGroup(group);
                }
            }
            if (newPost.getPost_class_id() != null) {
                Theclass theclass = theclassDao.queryById(newPost.getPost_class_id());
                if (theclass != null) {
                    newPost.setTheclass(theclass);
                }
            }

            for (OssImages ossImages : ossImagesList
            ) {
                Postphoto postphoto = new Postphoto();
                postphoto.setPost_id(newPost.getPost_id());
                postphoto.setImage_id(ossImages.getImage_id());
                postphotoDao.insertSelective(postphoto);
            }
            newPost.setOssImagesList(ossImagesList);
            baseModel.setSucceed("成功新增帖子", newPost);
        } else {
            baseModel.setFailure("新增帖子失败");
        }
    }


}
